package com.dody.agile.payment.classification;

import com.dody.agile.payment.model.Paycheck;
import com.dody.agile.payment.model.TimeCard;
import com.dody.agile.payment.utils.DateUtils;

import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ShiXin
 * @date 2019/4/11 19:37
 * @description agile
 */
public class HourlyClassification implements PaymentClassification {

    private static final Double normalHours = 8.0d;
    private double hourlySalary;
    private Map<Date, TimeCard> timeCards = new ConcurrentHashMap<>(31);


    @Override
    public double calculatePay(Paycheck paycheck) {
        /**
         * sum (每个时间卡 * 每小时报酬)
         * 对于钟点工，只计算过去一周的时间卡
         */
        double totalPay = 0;
        for (TimeCard tc : timeCards.values()) {
            if (DateUtils.between(tc.getDate(), paycheck.getPayPeriodStartDate(),
                    paycheck.getPayPeriodEndDate())) {
                totalPay += calculatePayForTimeCard(tc);
            }
        }
        return totalPay;
    }

    private double calculatePayForTimeCard(TimeCard tc) {
        Double hours = tc.getHours();
        if (Double.compare(hours, normalHours) > 0) {
            return 8 * hourlySalary + (hours - 8) * hourlySalary * 1.5;
        } else {
            return 8 * hourlySalary;
        }
    }


    public HourlyClassification(double hourlySalary) {
        this.hourlySalary = hourlySalary;
    }

    public double getHourlySalary() {
        return hourlySalary;
    }

    public void addTimeCard(TimeCard timeCard) {
        this.timeCards.put(timeCard.getDate(), timeCard);
    }

    public TimeCard getTimeCard(Long date) {
        return this.timeCards.get(date);
    }
}
